<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 订单类  
 */
class Order extends CI_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->library('request');  //自定义
        $this->load->library('response'); //自定义
        $this->load->library('session'); //公共 
    }


    /**
     * [createOrder 生成订单+支付订单----支付保证金]
     * @return [type] [description]
    */
    public function createTaskOrder(){
        
        if($this->input->method()=="post"){       
            $reqData = $this->request->getPostRawData();
            log_message('info', 'order/createOrder,data:'.json_encode($reqData)); 
            if(!$reqData['id']){
                $this->response->sendData(1000);return;
            }
            // var_dump($reqData);exit;
            //1验证登录状态
            $this->load->model("userModel");
            $retStatus =  $this->userModel->loginStatus($reqData);
            if($retStatus['status']){
                $this->response->sendData($retStatus['status']);return;

            }
            // var_dump($retStatus);exit;
            $userId = $retStatus['userId'];
            //2手机号
            $base =  $this->userModel->baseInfo($userId);
            if(!(isset($base->account) && $base->account)){
                $this->response->sendData(2018);return;                
            }
            if(!(isset($base->open_id) && $base->open_id)){
                $this->response->sendData(2018);return;                
            }
            $openId = $base->open_id;
            //3身份认证
            $extra = $this->userModel->extraInfo($userId);
            // var_dump($extra);exit;
            if(!(isset($extra->id_status)&&$extra->id_status==1)){
                $this->response->sendData(2019);return;                
            }
            //4邮箱
            if(!(isset($extra->id_status)&&$extra->email)){
                $this->response->sendData(2020);return;                
            }
            //5查询详情
            $this->load->model("taskModel");
            // var_dump($reqData);exit;
            $taskDetail =  $this->taskModel->taskDetail($reqData['id']);
            if(!isset($taskDetail['out_data']->id)){
                $this->response->sendData(3000);return;                  
            }

            $taskStatus = $taskDetail['out_data']->status;
            if($taskStatus==-2){ //被别人下单等待支付
                $this->response->sendData(3003);return;        
            }
            if($taskStatus!=0){
                //只有0可以下单
                $this->response->sendData(3001);return; 
            }
            //5.1 查询是否有未支付的订单
            $this->load->model("orderModel");
            $unPayedOrder =  $this->orderModel->unPayedOrder($userId);
            if($unPayedOrder){
                $this->response->sendData(3005);return;                 
            }
            
            //6可以领取任务，计算保障金
            if($this->config->item('percent')<0){
                $taskDeposit = $this->config->item('percentPrice');
            }
            else{
                $taskDeposit = floor($taskDetail['out_data']->reward * $this->config->item('percent'));
            }

            //7锁定订单(生成待支付订单、更改任务状态、调用微信接口生成订单)
            $reqData['orderNumber'] = $this->createOrderNumber($userId);
            $reqData['orderAmount'] = $taskDeposit;
            $reqData['openId'] = $openId;//'oKTiu4oImAyrHSOCQ4vAHcMk0AJg';
            $reqData['orderType'] = 1;//目前只有支付押金
            $reqData['baseUrl'] = $this->config->item('base_url');
            $this->load->model("orderModel");
            $orderStatus =  $this->orderModel->createOrder($userId,$reqData);
            if($orderStatus['status']){
                $this->response->sendData($orderStatus);return; 
            }
            $status = 0;              
            $out_data['payInfo'] = $orderStatus["payInfo"];
        }
        else{
            $status = 1001;
            $out_data = array();
        }
        $this->response->sendData($status,'',$out_data);

    }

    /**
     * [createOrderNumber description]
     * @return [type] [description]
     */
    private function createOrderNumber($userId){
        if($userId>1000000){
            $userId = $userId;
        }
        else{
            $userId +=1000000;
        }
        $number = $userId.time().rand(1000,9999);
        return $number;
    }   



    /**
     * [查询订单详情]
     * @return [type] [description]
     */
    public function orderDetail(){
        if($this->input->method()=="get"){       
            $reqData = $this->request->getGetData(array("orderNumber"));
            log_message('info', 'order/orderDetail,data:'.json_encode($reqData)); 

            //1验证登录状态 
            $this->load->model("userModel");
            $retStatus =  $this->userModel->loginStatus($reqData);
            // var_dump($retStatus);exit; 
            if($retStatus['status']){
                $this->response->sendData($retStatus['status']);return;
            }
            $userId = $retStatus['userId'];

            //2查询
            $this->load->model("orderModel");
            $detail = $this->orderModel->orderDetail($userId,$reqData['orderNumber']);
            // var_dump($list);exit;
            $ret['detail'] = $detail;
            $this->response->sendData(0,'',$ret);return ;    
            
          
        }
        else{
            $this->response->sendData(1001);return ;
        }
    }   


    /**
     * [notifyOrder description]
     * @return [type] [description]
     */
    public function notifyOrder(){
        log_message('info', 'order/notifyOrder'); 
        $this->load->model("orderModel");
        $ret = $this->orderModel->notifyOrder();
        log_message('info', 'order/notifyOrder,ret:'.json_encode($ret)); 
        $this->response->sendData($ret['status']);return ;

    }

    // /**
    //  * [orderStatus 给前段用，如果本地没有成功，查询第三方，如果第三方支付成功了，修改数据库]
    //  * @return [type] [description]
    //  */
    // public function orderStatus(){

    // }
    // 
    
    /**
     * [notifyRefundOrder 暂时不是必须的，退款接口会有状态返回]
     * @return [type] [description]
     */
    public function notifyRefundOrder(){
        // log_message('info', 'order/notifyRefundOrder'); 
        // $this->load->model("orderModel");
        // $ret = $this->orderModel->notifyRefundOrder();
        // log_message('info', 'order/notifyRefundOrder,ret:'.json_encode($ret)); 
        // $this->response->sendData($ret['status']);return ;

    }
    
}